package com.itheima.demo;

import com.itheima.domain.Person;
import com.itheima.mapper.PersonMapper;
import com.itheima.utils.MapperUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;


/*
    注解实现映射 测试
 */
public class AnnotationDemo {
    public static SqlSession sqlSession;
    public static PersonMapper mapper;

    //获取工具类对象
    MapperUtils mapperUtils = new MapperUtils();

    @Before
    public void before(){
        //通过工具类获取SqlSession对象
        sqlSession = mapperUtils.getSqlSession("MyBatisConfig.xml");
        mapper = sqlSession.getMapper(PersonMapper.class);
    }

    @After
    public void after(){
        //释放资源
        mapperUtils.closeResource();
    }

    @Test
    public void findAll(){
        List<Person> list = mapper.findAll();
        System.out.println(list);
    }

    @Test
    public void insertPerson(){
        Person p = new Person(null, "周七", 28);
        Integer insert = mapper.insert(p);
        System.out.println(insert);
    }

    @Test
    public void updatePerson(){
        Person p = new Person(4, "赵六", 26);
        Integer update = mapper.update(p);
        System.out.println(update);
    }

    @Test
    public void deletePerson(){
        Integer delete = mapper.delete(6);
        System.out.println(delete);
    }

}
